﻿// Write a program that extracts from given XML file all the text without the tags. 

using System;
using System.IO;
using System.Text.RegularExpressions;

namespace _10.ExtractAllDataFromXML
{
    class ExtractAllDataFromXML
    {
        static void Main()
        {
            string path = @"..\..\";

            string inputFileName = "input.xml";
            string inputFile = Path.Combine(path, inputFileName);

            string outputFileName = "xmlValues.txt";
            string outputFile = Path.Combine(path, outputFileName);

            string pattern = "(?<=>)(.*)(?=</)";
            try
            {
                ExtractMatchPatternToFile(inputFile, outputFile, pattern);
                Console.WriteLine("All xml tag values are extracted in file: {0}", outputFile);
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }

        private static void ExtractMatchPatternToFile(string inputFile, string outputFile, string pattern)
        {
            using (StreamReader reader = new StreamReader(inputFile))
            using (StreamWriter writer = new StreamWriter(outputFile))
            {
                while (!reader.EndOfStream)
                {
                    string currentLine = reader.ReadLine();
                    foreach (Match match in Regex.Matches(currentLine, pattern))
                    {
                        writer.WriteLine(match);
                    }
                }
            }
        }
    }
}